package com.yjs.app.trade.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 14-2-4.
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{

	/**
	 * 将字符串转换为整型数
	 * @param str
	 * @param defaultValue	出现异常时的默认值
	 * @return
	 */
	public static int getInt(String str, int defaultValue) {
		int returnInt = defaultValue;

		try {
			returnInt = Integer.parseInt(str);
		} catch (Exception e) {
			returnInt = defaultValue;
		}
		return returnInt;
	}
	
	/**
     * js的escape java实现
     * @param src	源字符串
     * @return
     */
    public static String escape(String src) {
		int i;
		char j;
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length() * 6);
		for (i = 0; i < src.length(); i++) {
			j = src.charAt(i);
			if (Character.isDigit(j) || Character.isLowerCase(j)
					|| Character.isUpperCase(j))
				tmp.append(j);
			else if (j < 256) {
				tmp.append("%");
				if (j < 16)
					tmp.append("0");
				tmp.append(Integer.toString(j, 16));
			} else {
				tmp.append("%u");
				tmp.append(Integer.toString(j, 16));
			}
		}
		return tmp.toString();
	}

    /**
     * js的unescape java实现
     * @param src	源字符串
     * @return
     */
	public static String unescape(String src) {
		StringBuffer tmp = new StringBuffer();
		tmp.ensureCapacity(src.length());
		int lastPos = 0, pos = 0;
		char ch;
		while (lastPos < src.length()) {
			pos = src.indexOf("%", lastPos);
			if (pos == lastPos) {
				if (src.charAt(pos + 1) == 'u') {
					ch = (char) Integer.parseInt(src
							.substring(pos + 2, pos + 6), 16);
					tmp.append(ch);
					lastPos = pos + 6;
				} else {
					ch = (char) Integer.parseInt(src
							.substring(pos + 1, pos + 3), 16);
					tmp.append(ch);
					lastPos = pos + 3;
				}
			} else {
				if (pos == -1) {
					tmp.append(src.substring(lastPos));
					lastPos = src.length();
				} else {
					tmp.append(src.substring(lastPos, pos));
					lastPos = pos;
				}
			}
		}
		return tmp.toString();
	}
	
	/**
	 * 判断字符串是否包含中文
	 * @param str
	 * @return
	 */
	public static boolean isChinese(String str) {
		String regEx = "[\\u4e00-\\u9fa5]+";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(str);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 判断字符串是否全为中文
	 * @param str
	 * @return
	 */
	public static boolean isAllChinese(String str) {
		String reg = "[\\u4e00-\\u9fa5]+";
		return str.matches(reg);
	}
	
	/**
	 * 判断字符串中包含的特殊字符的次数
	 */
	public static int getSpecialNum (String str , String des) {
        int cnt = 0;
        int offset = 0;
        while((offset = str.indexOf(des, offset)) != -1){
            offset = offset + des.length();
            cnt++;
        }
        return cnt;
	}
	
	public static boolean strIsNotNull (Object object) {
		if (object != null && isNotBlank(object.toString())) {
			return true;
		} else {
			return false;
		}
	}
	
	public static boolean strIsNull (Object object) {
		if (object == null || isBlank(object.toString())) {
			return true;
		} else {
			return false;
		}
	}
}
